<?php
namespace App\Model;

use App\Repository\Sys\Db;
use App\Repository\Sys\JsonRespose;
use App\Repository\Sys\Str;
use App\Repository\Sys\Sys;

class SmsCodeTable
{
    use Db;
    static private $table = 'sms_code';

    static public function sendCode($phone)
    {
        $code = rand(1000, 9999);


//        if (config('app.debug')) {
//            $code=123456;  // 测试短信
//        }

        $res = SmsCodeTable::ORM()->insert(array(
            'sms_code_phone' => $phone,
            'sms_code_val' => $code,
            'sms_code_status' => 1,
            'created_at' => Str::getNowDateTime(),
            'updated_at' => Str::getNowDateTime(),
        ));

        if (!$res) {
            return JsonRespose::jsonArray(0);
        } else {

            //  发送验证码3056821
            $send = Sys::SendMessage(3942382,[$phone],[$code]);  // 确认
//            $send = Sys::SendMessage(3057933,[$phone],['123','维修']);
            if ($send['code']==1) {
                return JsonRespose::jsonArray(1);
            } else {
                return JsonRespose::jsonArray(0, null, '调用短信服务失败,请联系技术人员');
            }
        }

    }
}